var date=echartUtil.GetDateStr(0);
var startDate=echartUtil.GetDateStr(-10);
//相关业务逻辑
$(function(){
	page.bindTag();
	$("#startDay").val(startDate);
	$("#yestarday").val(date);
});


function dataChange(){
	startDate=$("#startDay").val();
	date=$("#yestarday").val();
	var orderID=$("#orderInput").val();
	page.getOrder(orderID,startDate,date);
	page.getOrderWork(orderID);
}


var page={//页面总变量
		bindTag:function(){
			$("#queryOrderBtn").bind("click",function(){
				var orderID=$("#orderInput").val();
				page.getOrder(orderID,startDate,date);
				page.getOrderWork(orderID);
			});
		},
		orderBind:function(){
			$(".btn").bind("click",function(){
				var id=$(this).text();
				var className=$(this).attr('class');
				if(className.indexOf('orderHid')>0){
					$(this).addClass("orderShow");
					$(this).removeClass("orderHid");
					$("."+id).hide("slow");
				}else{
					$(this).addClass("orderHid");
					$(this).removeClass("orderShow");
					$("."+id).show("slow");
				}
			});
		},
		getOrder:function(id,start,end){
			ajaxUtil.ajaxRuquest('getOrderDetail',{'orderId':id,'startDate':start,'date':end},function(data){
				var html="";
				for(var i=0;i<data.length;i++){
					html+="<div class='clearfix'></div><div class='center'><span>订单编号</span><span class='btn orderHid'>"+data[i].orderId
					+"</span><span>订单总数</span><span class='btn'>"+data[i].orderCount
					+"</span><span>工厂型号</span><span class='btn'>"+data[i].factoryId
					+"</span><span>客户编号</span><span class='btn'>"+data[i].customerId
					+"</span><span>客户订单编号</span><span class='btn'>"+data[i].customerOrderId
					+"</span></div><h3></h3><div class="+data[i].orderId
					+"></div>";
				}
				$("#allOrder").html("");
				$("#allOrder").append(html);
				page.orderBind();
			});
		},
		getOrderWork:function(id){
			ajaxUtil.ajaxRuquest('getOrderWork',{'orderId':id},function(data){
				//遍历所有订单
				var sort=data[0]["order"];
				console.log(sort);
				for(var i=1;i<data.length;i++){
					var html="";
					var finish="<tr><td colspan=5>车间生产完成</td></tr>";
					var orderData=data[i];//一个订单所有数据
					var orderId="";
					var outWorkShop=false;
					//得到所有车间
					for(var a=0;a<sort.length;a++){
						var workHtml="";
						var workShopData=orderData[sort[a]];//得到车间数据
						if(workShopData.length>0){
							workHtml+="<div class='col-md-4'> <span class='btn text'>"+sort[a]+"</span><div class='workDiv'><table class='table table-border table-bordered table-bg table-hover'>";
							workHtml+=finish;
						}
						for(var e=0;e<workShopData.length;e++){
							workHtml+="<tr><td>"+workShopData[e].procedur+
							"</td><td>"+workShopData[e].maching+
							"</td><td>"+workShopData[e].colorDteil+
							"</td><td>"+workShopData[e].delayTime+
							"</td><td>"+workShopData[e].qualifiedCount+"</td></tr>"
							if(workShopData[e].orderId!=""){
								orderId=workShopData[e].orderId;
							}
							if(workShopData[e].procedur=='交接出库'&&workShopData[e].qualifiedCount>=workShopData[e].orderCount){
								outWorkShop=true;
							}
						}
						console.log(outWorkShop);
						if(outWorkShop){
							outWorkShop=false;
						}else{
							workHtml=workHtml.replace(finish,"");
						}
						workHtml+="</table></div></div>"
						html+=workHtml;
					}
					if(orderId!=""){
						$("."+orderId).append(html);
					}
					
				}
			});
		}
}
